package Controller;

import Dao.AdministratorDao;
import Dao.SignupDao;
import Dao.StaffDao;
import Dao.WithAdDao;
import Entity.Officer;
import Utils.SimpleTools;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class WithAdController extends Application implements Initializable {
    Stage primaryStage = new Stage();

    @FXML
    AnchorPane addPane;
    @FXML
    private TextField password;
    @FXML
    private TextField username;
    @FXML
    private TextField speciality;
    @FXML
    private TextField type;
    @FXML
    private TextField id;
    @FXML
    private TextField name;
    @FXML
    private TextField phone;

    @FXML
    AnchorPane searchPane;
    @FXML
    private TextField nameSearch;
    @FXML
    private TableView<Officer> table;
    @FXML
    private TableColumn<Officer, String> nametable;
    @FXML
    private TableColumn<Officer, String> specialitytable;
    @FXML
    private TableColumn<Officer, String> typetable;
    @FXML
    private TableColumn<Officer, String> idtable;
    @FXML
    private TableColumn<Officer, String> phonetable;

    @FXML
    private Button titleButton;
    @FXML
    private Label titleLabel;

    public WithAdController() throws IOException {
    }

    public Button getTitleButton() {
        return titleButton;
    }
    public Label getTitleLabel() {
        return titleLabel;
    }

    ObservableList<Officer> celldata = FXCollections.observableArrayList(AdministratorDao.getStaff());
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle){
        if(StaffDao.getInstance().getOperator()==1){
            getTitleButton().setText("添加");
            getTitleLabel().setText("添加职工");
            addPane.setVisible(true);
        }
        else{
            getTitleButton().setText("查询");
            getTitleLabel().setText("查询职工");

            nametable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
            specialitytable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getSpeciality()));
            typetable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getType()));
            idtable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId()));
            phonetable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPhone()));

            searchPane.setVisible(true);
        }
    }
    @FXML
    public void titleButton(ActionEvent event) throws IOException{
        if(StaffDao.getInstance().getOperator()==1){
            //添加职工
            String[] properties = {username.getText(),password.getText(),name.getText(),
                    id.getText(), speciality.getText(),type.getText(),phone.getText()};
            if(SimpleTools.isempty(properties)) SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请输入全部信息");
            else{
                if(id.getText().length()!=18){
                    SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请正确输入身份证号");
                }
                else {
                    if(SignupDao.iscontain(properties[0])){
                        SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "抱歉", "该用户名已注册");
                    }
                    else{
                        SignupDao.logup(properties);
                        username.clear();
                        password.clear();
                        name.clear();
                        id.clear();
                        speciality.clear();
                        type.clear();
                        phone.clear();
                        SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "", "添加成功！");
                    }
                }
            }
        }
        else{
            //查询职工

            celldata.clear();
            celldata.addAll(WithAdDao.byName(nameSearch.getText()));
            table.setItems(null);
            table.setItems(celldata);
        }
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        this.primaryStage = primaryStage;

        Parent root = FXMLLoader.load(getClass().getResource("../View/WithAdController.fxml"));
        primaryStage.setTitle("NEU颐养中心");
        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.getIcons().add(new Image("file:src/resource/flour.png"));
        primaryStage.showAndWait();
    }
    public void  showWindow() throws Exception {
        start(primaryStage);
    }
    public static void main(String[] args) {
        launch(args);
    }

}
